package com.diko.ucenter.controller;


import com.diko.commonUtils.JwtUtils;
import com.diko.commonUtils.R;
import com.diko.commonUtils.vo.UcenterMemberOrder;
import com.diko.config.exceptionhandler.myException;
import com.diko.ucenter.entity.Member;
import com.diko.ucenter.entity.vo.LoginVo;
import com.diko.ucenter.entity.vo.RegisterVo;
import com.diko.ucenter.service.MemberService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author Emma-Li
 * @since 2020-10-15
 */
@CrossOrigin
@RestController
@RequestMapping("/educenter/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @ApiOperation(value = "会员登录")
    @PostMapping("login")
    public R login(@RequestBody LoginVo loginVo){
        String token=memberService.login(loginVo);
        return R.ok().data("token",token);
    }

    @ApiOperation(value = "会员注册")
    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo){
        memberService.register(registerVo);
        return R.ok();
    }

    @ApiOperation(value = "根据token获取登录信息")
    @GetMapping("auth/getLoginInfo")
    public R getLoginInfo(HttpServletRequest request){
        try {
            String memberId = JwtUtils.getMemberIdByJwtToken(request);
            LoginVo loginVo = memberService.getLoginInfo(memberId);
            return R.ok().data("item", loginVo);
        }catch (Exception e){
            e.printStackTrace();
            throw new myException(20001,"token获取信息失败");
        }
    }

    @PostMapping("getInfoUc/{id}")
    public UcenterMemberOrder getInfo(@PathVariable String id){
        Member  member= memberService.getById(id);
        UcenterMemberOrder ucenterMember = new UcenterMemberOrder();
        BeanUtils.copyProperties(member,ucenterMember);
        return ucenterMember;
    }

    @GetMapping(value = "countRegister/{day}")
    public R countRegister(
            @PathVariable String day
    ){
        Integer count=memberService.countRegisterByDay(day);
        return R.ok().data("countRegister",count);
    }
}

